package com.ssm.controller.teacher;

import com.alibaba.fastjson.JSONObject;
import com.ssm.controller.BaseController;
import com.ssm.entity.CourseZhanshi;
import com.ssm.entity.Selectcourse;
import com.ssm.entity.UmStudent;
import com.ssm.service.CourseService;
import com.ssm.service.SelectCourseService;
import com.ssm.service.StudentService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
public class TeacherMarkController extends BaseController {
    @Resource(name = "selectCourseService")
    private SelectCourseService selectCourseService;
    @Resource(name = "courseService")
    private CourseService courseService;
    @Resource(name = "studentService")
    private StudentService studentService;
    // 查询
    @RequestMapping(value ="markman/query",method = RequestMethod.GET)
    public String MarkQuery(HttpSession session, Map<String,Object> map){
        List<Selectcourse> list=new ArrayList<>();
        logger.info("检查权限");
        String adminId=checkAdmin(session);
        if (!adminId.equals("teacherId")){
            return "redirect:/";
        }


        List<CourseZhanshi> zhanshiList=new ArrayList<>();
        list=selectCourseService.listAllSelectCourse();
        for (int i=0;i<list.size();i++){
            CourseZhanshi zhanshi=new CourseZhanshi();
            Selectcourse selectcourse= list.get(i);
            logger.info("--++++++---->"+selectcourse);
            UmStudent umStudent= studentService.getUmStudent(selectcourse.getId());
            logger.info("------------>"+umStudent);
            zhanshi.setKechengname( courseService.Coursename(selectcourse.getCourseid()));
            if (!(umStudent==null)) {
                zhanshi.setStudengname(umStudent.getName());
            }
            zhanshi.setCourseid(selectcourse.getCourseid());
            if (selectcourse.getMark()!=null) {
                zhanshi.setChengji(selectcourse.getMark().toString());
            }
            zhanshi.setXueshengid(selectcourse.getId());
            zhanshiList.add(zhanshi);
        }
        map.put("zhanshiList",zhanshiList);
        for (int i = 0; i < zhanshiList.size(); i++) {
            logger.info(zhanshiList.get(i));
        }
        return "teacher/mark";
    }


    // 打分
    @ResponseBody
    @RequestMapping(value ="markman/add",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    public String Markadd(HttpSession session, @RequestParam String fenshu, @RequestParam Integer xueshengid, @RequestParam Integer kechengbianhao){
        List<Selectcourse> list=new ArrayList<>();
        logger.info("检查权限");
        String adminId=checkAdmin(session);
        if (!adminId.equals("teacherId")){
            return "redirect:/";
        }


        Integer i = null;
        if(fenshu!=null){
            i = Integer.valueOf(fenshu);
        }

        Selectcourse selectcourse=new Selectcourse();
        selectcourse.setCourseid(kechengbianhao);
        selectcourse.setId(xueshengid);
        selectcourse.setMark(i);
        Integer IsSuccuess=selectCourseService.update(selectcourse);
        JSONObject object=new JSONObject();
        if (IsSuccuess==1){
            logger.info("添加成功");

            object.put("status","success");
        }else{
            logger.info("添加错误");
            object.put("status","false");
        }
        return object.toJSONString();
    }
}
